package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.api.CovidRiskZoneApi;
import com.example.demo.entity.BanJi;
import com.example.demo.entity.Communitys;
import com.example.demo.service.CommunitysService;
import com.example.demo.vo.CommunitysVo;
import com.example.demo.vo.DataView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Coding路人王
 * @date 2022/11/24 22:04
 */
@Controller
@Slf4j
public class CovidRiskZoneController {

    @Autowired
    private CommunitysService communitysService;

    /**
     *  入参：
     *  token	true	string	请求token，用户中心获取。	用户中心获取token
     * province	false	string	省份，对应key级别为省级	北京市
     * city		string	市级，对应key为地级	东城区
     * county		string	县级，对应key为县级	东华门街道
     *
     * 出参：
     * end_update_time	最后更新时间
     * high_count	高风险地区数量
     * middle_count	中风险地区数量规范
     * high_list	高风险地区列表
     * middle_list	中风险地区列表
     */

    // /zone/toZone
    @RequestMapping("/zone/toZone")
    public String toZone(){
        return "zone/zone";
    }


    @RequestMapping("/zone/searchRiskZone")
    @ResponseBody
    public DataView searchCovidRiskZoneData(CommunitysVo communitysVo,String province, String city, String county) throws IOException {

        // 必填项
        if (StringUtils.isEmpty(province)){
            province = "北京市";
        }


        // 1.解析接口数据
        String riskZoneData = CovidRiskZoneApi.getCovidRiskZoneData(province,city,county);

        // 2.拿到社区的数据封装List集合
        List<Communitys> list = new ArrayList<>();

        // fastjson解析实体
        if (StringUtils.isNotEmpty(riskZoneData)){

            JSONObject jsonObject = JSONObject.parseObject(riskZoneData);
            JSONObject data = jsonObject.getJSONObject("data");
            log.info("风险地区接口返回数据：{}",data.toString());
            //Integer high_count = (Integer) data.get("high_count");
            Integer high_count = data.getInteger("high_count");
            String end_update_time = (String) data.get("end_update_time");
            log.info("高风险地区截止统计时间：{}",end_update_time);
            log.info("高风险地区总数：{}",high_count);
            // 解析
            JSONArray high_list = data.getJSONArray("high_list");
            log.info("高风险地区 high_list：{}", high_list);
            for (int i = 0; i < high_list.size(); i++) {
                JSONObject high_listJSONObject = high_list.getJSONObject(i);
                String area_name = (String) high_list.getJSONObject(i).get("area_name");
                log.info("高风险地区 :{}",high_listJSONObject);
                JSONArray communitys = high_listJSONObject.getJSONArray("communitys");
                for (int j = 0; j < communitys.size(); j++) {
                    String s = communitys.get(j).toString();
                    Communitys community = new Communitys();
                    community.setCommunity(s);
                    community.setTime(end_update_time);
                    community.setCount(high_count);
                    community.setArea_name(area_name);
                    list.add(community);
                }
            }

            // 3.做好分页配置
//            IPage<Communitys> iPage = new Page<>(communitysVo.getPage(),communitysVo.getLimit());
//            IPage<Communitys> page = communitysService.page(iPage);
//            page.setRecords(list);
//            page.setTotal(list.size());
//            log.info("list~~~~~~~~~~~~~~~{}",iPage.getRecords().toString());
//            System.out.println(iPage.getRecords().toString());

            return new DataView(list);
        }


        return null;
    }

//    public static void main(String[] args) throws IOException {
//        searchCovidRiskZoneData(null,"北京市",null,null);
//        searchCovidRiskZoneData(null,"北京市","朝阳区",null);
//        searchCovidRiskZoneData(null,"北京市","朝阳区","永安里社区");
//    }
}
